using System;
using System.Collections.Generic;
using System.Text;

//using CardControl.CCard;

namespace BlackjackGUI
{
   class CShoe
   {
      internal CShoe()
      {
         m_shoe = new LinkedList<CCard>();
         m_discard = new LinkedList<CCard>();
      }

      internal CShoe(int numDecks)
      {
         m_numDecks = numDecks;
         m_shoe = new LinkedList<CCard>();
         m_discard = new LinkedList<CCard>();

         for (int ii = 0; ii < numDecks; ii++)
         {
            LinkedList<CCard> temp = new CDeck().GetAllCards();
            for (int jj = 0; jj < 52; jj++)
            {
               m_shoe.AddFirst(temp.First.Value);
               temp.RemoveFirst();
            }
         }

         Shuffle();
      }

      ~CShoe()
      {
         m_shoe.Clear();
         m_shoe = null;
      }

      internal void Shuffle()
      {
         // Combine all cards in the discard pile
         while (m_shoe.Count > 0)
         {
            m_discard.AddLast(m_shoe.First.Value);
            m_shoe.RemoveFirst();
         }

         // Randomly move cards back to shoe
         Random rand = new Random();
         while (m_discard.Count > 0)
         {
            int index = rand.Next(m_discard.Count);
            LinkedListNode<CCard> iter = m_discard.First;
            for (int ii = 0; ii < index; ii++)
            {
               iter = iter.Next;
            }
            m_shoe.AddLast(iter.Value);
            m_discard.Remove(iter);
         }
      }

      internal CCard DealOneCard()
      {
         CCard retVal;
         retVal = m_shoe.First.Value;
         m_shoe.RemoveFirst();
         m_discard.AddLast(retVal);
         return retVal;
      }

      internal int ShoePercentRemaining()
      {
         return Convert.ToInt32(m_shoe.Count * 100 / (m_numDecks * 52));
      }

      private LinkedList<CCard> m_shoe;
      private LinkedList<CCard> m_discard;
      private int m_numDecks;
   }
}
